package outbound_account

import (
	"context"
	dto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/market"
	marketResp "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/repository/market"
	pb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/market"
	"git.myscrm.cn/golang/stark/v4"
)

type OutboundAccountService struct {
	outboundAccountRepo marketResp.OutboundAccountRepositoryIface
}

func NewOutboundAccountService(
	outboundAccountRepo marketResp.OutboundAccountRepositoryIface,
) OutboundAccountServiceIface {
	return &OutboundAccountService{
		outboundAccountRepo: outboundAccountRepo,
	}
}

func (s *OutboundAccountService) GetRobotList(ctx context.Context, companyId int64) ([]dto.AccountRobotItem, error) {
	params := &pb.GetRobotListRequest{
		CompanyId: companyId,
	}
	robotList, err := s.outboundAccountRepo.GetRobotList(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetRobotList error, err:%+v", err)
		return nil, err
	}
	response := make([]dto.AccountRobotItem, 0)
	for _, robot := range robotList.List {
		response = append(response, dto.AccountRobotItem{
			IndustryOneName: robot.IndustryOneName,
			DeployTime:      robot.DeployTime,
			IndustryTwoName: robot.IndustryTwoName,
			RobotDefId:      robot.RobotDefId,
			RobotName:       robot.RobotName,
			RobotStatus:     robot.RobotStatus,
		})
	}
	return response, nil
}

func (s *OutboundAccountService) GetPhoneList(ctx context.Context, companyId int64) ([]dto.AccountPhoneItem, error) {
	params := &pb.GetPhoneListRequest{
		CompanyId: companyId,
	}
	phoneList, err := s.outboundAccountRepo.GetPhoneList(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetPhoneList error, err:%+v", err)
		return nil, err
	}
	response := make([]dto.AccountPhoneItem, 0)
	for _, phoneInfo := range phoneList.List {
		response = append(response, dto.AccountPhoneItem{
			UserPhoneId: phoneInfo.UserPhoneId,
			PhoneName:   phoneInfo.PhoneName,
			PhoneNum:    phoneInfo.PhoneNum,
			PhoneType:   phoneInfo.PhoneType,
		})
	}
	return response, nil
}

func (s *OutboundAccountService) GetSeatInfoList(ctx context.Context, companyId int64) (*dto.AccountSeatInfoResponse, error) {
	params := &pb.GetSeatInfoListRequest{
		CompanyId: companyId,
	}
	seatInfoList, err := s.outboundAccountRepo.GetSeatInfoList(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetSeatInfoList error, err:%+v", err)
		return nil, err
	}
	return &dto.AccountSeatInfoResponse{
		AllCallSeat:   seatInfoList.AllCallSeat,
		UsingCallSeat: seatInfoList.UsingCallSeat,
	}, nil
}

func (s *OutboundAccountService) GetAccountDetail(ctx context.Context, request *dto.GetAccountDetailRequest) (*dto.GetAccountDetailResponse, error) {
	var id int64
	if request.Id > 0 {
		id = request.Id
	} else {
		id = 0
	}
	detail, err := s.outboundAccountRepo.GetAccountDetail(ctx, &pb.GetAccountDetailRequest{
		Id: id,
	})
	if err != nil {
		stark.Logger.Errorf(ctx, "GetAccountDetail error, err:%+v", err)
		return nil, err
	}
	if detail.Id == 0 {
		return nil, nil
	}

	return &dto.GetAccountDetailResponse{
		Id:          detail.Id,
		AccountName: detail.AccountName,
		CompanyId:   detail.CompanyId,
		Status:      detail.Status,
		CreatedOn:   detail.CreatedOn,
	}, nil
}

func (s *OutboundAccountService) SaveAccount(ctx context.Context, userId int64, request *dto.SaveAccountRequest) (*dto.SaveAccountResponse, error) {
	params := &pb.SaveAccountRequest{
		Id:          request.Id,
		AccountName: request.AccountName,
		CompanyId:   request.CompanyId,
		UserId:      userId,
	}
	resp, err := s.outboundAccountRepo.SaveAccount(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "SaveAccount error, err:%+v", err)
		return nil, err
	}

	//err = s.outboundAccountRepo.SyncAccountToBasicData(ctx, authorize.GetTenantCode(ctx), request.CompanyId)
	//if err != nil {
	//	stark.Logger.Errorf(ctx, "SyncAccountToBasicData error, err:%+v", err)
	//	return nil, err
	//}

	return &dto.SaveAccountResponse{Id: resp.Id}, nil
}

func (s *OutboundAccountService) ExpenseDetail(ctx context.Context, request *pb.ExpenseDetailRequest) (*dto.ExpenseDetailResponse, error) {
	resp, err := s.outboundAccountRepo.ExpenseDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "ExpenseDetail error, err:%+v", err)
		return nil, err
	}
	respdto := &dto.ExpenseDetailResponse{
		MinutesAmount: resp.MinutesAmount,
		BillingModel:  resp.BillingModel,
		SeatsNum:      resp.SeatsNum,
		UnitPrice:     resp.UnitPrice,
		DetailList:    nil,
	}
	respdtoDetailList := make([]*dto.ExpenseDetail, 0)
	for _, v := range resp.DetailList {
		respdtoDetailList = append(respdtoDetailList, &dto.ExpenseDetail{
			Date:            v.Date,
			DurationMinutes: v.DurationMinutes,
			UnitPrice:       v.UnitPrice,
		})
	}
	respdto.DetailList = respdtoDetailList
	return respdto, nil
}
